package hash_map;

import java.util.HashSet;
import java.util.Set;

// 两个数的交集
public class leetcode349 {
    public static void main(String[] args) {
        int[] nums1 = {1,2,2,1};
        int[] nums2 = {2,2};
        int[] result = intersection(nums1, nums2);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for (int i = 0; i < nums1.length; i++) {
            set1.add(nums1[i]);
        }

        for (int i = 0; i < nums2.length; i++) {
            if(set1.contains(nums2[i])) set2.add(nums2[i]);
        }

        int[] result = new int[set2.size()];
        int n = 0;
        int j = 0;
        for (Integer i : set2) {
            result[j++] = i;
        }
        return result;
    }
}
